package kp.rpc.Enum;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Getter
public enum ProtocolMessageSerializerEnum {
    JDK(0, "jdk"),
    JSON(1, "json"),
    KRYO(2, "kryo"),
    HESSIAN(3, "hessian");
    private final int key;
    private final String value;

    ProtocolMessageSerializerEnum(int key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 获取值列表
     */
    public static List<String> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }

    public static ProtocolMessageSerializerEnum getEnumByKey(int key) {
        ProtocolMessageSerializerEnum[] values = ProtocolMessageSerializerEnum.values();
        for (ProtocolMessageSerializerEnum protocolMessageSerializerEnum : values) {
            if (protocolMessageSerializerEnum.key == key) {
                return protocolMessageSerializerEnum;
            }
        }
        return null;
    }

    public static ProtocolMessageSerializerEnum getEnumByValue(String value) {
        if (StrUtil.isEmpty(value)) {
            return null;
        }
        ProtocolMessageSerializerEnum[] values = ProtocolMessageSerializerEnum.values();
        for (ProtocolMessageSerializerEnum protocolMessageSerializerEnum : values) {
            if (Objects.equals(protocolMessageSerializerEnum.value, value)) {
                return protocolMessageSerializerEnum;
            }
        }
        return null;
    }
}
